﻿using System;
using System.Web;
using Entity;
using BLL;
using BLL.Access;
using System.Data;
using Web.Class;
using System.IO;
using YXYLCommon;

namespace Web.Pages.YongShui._13.LAIHANGL
{
    public partial class GAOPAIUPLOAD : BasePage
    {
        #region 属性
        private string urltype;
        private string userId;
        private int wtype;
        private string yonhuh;
        private string m_laihanbh;
        #endregion

        #region 变量

        /// <summary>
        /// 上传文件
        /// </summary>
        private SYS_UPLOADProcess bllupload = new SYS_UPLOADProcess();


        /// <summary>
        /// 用户信息BLL 水资源
        /// </summary>
        private QS_YG_YONGHUXXBLL _QSYHXXBLL = new QS_YG_YONGHUXXBLL();

        private SYS_UPLOADProcess uploadbll = new SYS_UPLOADProcess();

        private SYS_UPLOAD_LOGBLL uploadLOGBLL = new SYS_UPLOAD_LOGBLL();

        #endregion

        #region 事件

        protected void Page_Load(object sender, EventArgs e)
        {
            this.wtype = string.IsNullOrEmpty(Request["wtype"]) ? 0 : Convert.ToInt32(Request["wtype"]);
            this.urltype = Request["urltype"];
            this.userId = Request["userId"];
            this.yonhuh = Request["yonghuh"];
            this.m_laihanbh = Request["laihanbh"].ToString();
            this.wtype = string.IsNullOrEmpty(Request["wtype"]) ? 0 : Convert.ToInt32(Request["wtype"]);
            if (Request["type"] == "upload")
            {
                Response.Write(UploadGP());
                Response.End();
            }
            
        }

        #endregion

        #region 方法

        /// <summary>
        /// 高拍上传
        /// </summary>
        /// <returns></returns>
        private string UploadGP()
        {
            string filepath = PageTools.GetUploadPath(wtype, urltype);
            HttpPostedFile imgFile = Request.Files["File"];
            if (imgFile == null)
            {
                return "-1";
            }
            String fileName = imgFile.FileName;
            String fileExt = Path.GetExtension(fileName).ToLower();
            filepath += DateTime.Now.ToString("yyyymmddhhmmssfff") + fileExt;


            imgFile.SaveAs(Server.MapPath(filepath));

            if (Session["gaopai"] != null)
            {
                DataTable dt = Session["gaopai"] as DataTable;
                DataRow dr = dt.NewRow();
                dr[0] = filepath;
                dr[1] = dt.Rows.Count + 1;
                dr[2] = Request["txtname"];
                dr[3] = Request["txtvalue"];
                dr[4] = 2;
                dt.Rows.Add(dr);
                Session["gaopai"] = dt;
                SaveUpload(dt);
                
            }
            else
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("path");
                dt.Columns.Add("rownum");
                dt.Columns.Add("name");
                dt.Columns.Add("value");
                dt.Columns.Add("type");
                DataRow dr = dt.NewRow();
                dr[0] = filepath;
                dr[1] = dt.Rows.Count + 1;
                dr[2] = Request["txtname"];
                dr[3] = Request["txtvalue"];
                dr[4] = 2;
                dt.Rows.Add(dr);
                Session["gaopai"] = dt;
                SaveUpload(dt);
            }
            Session["gaopai"] = null;
            return filepath;
        }

        private void SaveUpload(DataTable uploadtable)
        {
            //var yonhuh = Request["yonghuh"].ToString();
            var userId = Request["userId"].ToString();
            SYS_UPLOAD upload = new SYS_UPLOAD();
            upload.I_SUOSHUMK = this.urltype.ToUpper() == "LH" ? 1 : 0 ;
            upload.I_TYPE = this.wtype;
            upload.S_CAOZUOR = userId;// LoginInfo.CurrentUser.UserID.ToString();
            upload.S_LAIHANBH = string.IsNullOrEmpty(this.m_laihanbh) ? "" : this.m_laihanbh;
            upload.S_YONGHUBH = yonhuh;
            upload.D_CAOZUORQ = DateTime.Now;
            upload.I_SHANGCHUANLB = 2;
            foreach (DataRow dr in uploadtable.Rows)
            {
                upload.ID = 0;
                ///证件类型的文件只和用户相关，更新

                DataTable shangchuan =
                    uploadbll.SelectSYS_UPLOADsDynamic(
                        string.Format(" I_JLZT=0 AND S_YONGHUBH='{0}' and I_WENJIANLX={1} AND S_LAIHANBH IS NULL", yonhuh, dr["value"]), "", null);
                if (shangchuan != null && shangchuan.Rows.Count > 0)
                {
                    upload.ID = Convert.ToInt32(shangchuan.Rows[0]["ID"]);
                }

                upload.S_WENJIANMC = dr["name"].ToString();
                upload.S_WENJIANLJ = dr["path"].ToString();
                upload.I_WENJIANLX = Convert.ToInt32(dr["value"]);
                //upload.I_SHANGCHUANLB = Convert.ToInt32(dr["type"]);
                uploadbll.InsertOrUpdate(upload);
                SYS_UPLOAD_LOG uploadlog = new SYS_UPLOAD_LOG();
                uploadlog.D_CAOZUORQ = DateTime.Now;
                uploadlog.I_CAOZUOR = userId.GetNullabelDecimal();
                uploadlog.I_JLZT = 0;
                uploadlog.I_TYPE = BusinessTool.WType; 
                uploadlog.I_WENJIANLX = Convert.ToInt32(dr["value"]);
                uploadlog.ID = 0;
                uploadlog.S_WENJIANLJ = dr["path"].ToString();
                uploadlog.S_WENJIANMC = dr["name"].ToString();
                uploadlog.S_YONGHUBH = yonhuh;
                uploadLOGBLL.InsertOrUpdate(uploadlog);

                new SYS_LOGBLL().Log<SYS_UPLOAD>(userId,this.wtype.ToString(),
                    "用户资料上传", OperateState.Modify, upload, null);

            }

            #endregion
        }
    }
}